﻿artApp.factory('ArtService', function ($http, $q) {
    var artists = [];
    var artPieces = [];
    var admins = [];
    var loggedInUser = {};

    var getArtists = function () {
        var deferred = $q.defer();
        $http.get("/api/Artist").success(function (data) {
            artists.length = 0;
            for (var a in data) {
                if (data[a].IsAdmin) {
                    admins.push(data[a]);
                }
                else {
                    artists.push(data[a]);
                }
                if (data[a].LoggedIn) {
                    loggedInUser = data[a];
                }
            }
            deferred.resolve(artists);
        }).error(function (data, status) {
            console.log(status);
            deferred.reject();
        })
        return deferred.promise;
    }

    var isAdminLoggedIn = function()
    {
        if (Object.keys(loggedInUser).length == 0) {
            return false;
        }
        else {
            return loggedInUser.IsAdmin;
        }
    }

    var isUserLoggedIn = function(id)
    {
        if (Object.keys(loggedInUser).length == 0) {
            return false;
        }
        else {
            return (loggedInUser.ArtistId == id);
        }        
    }
 
    var getArtist = function (id) {
        var artist = {};
        for(var i = 0; i < artists.length; i++)
        {
            artist = artists[i];
            if (artist.ArtistId == id) {
                return artist;
            }
        }
        return artist;
    }

    var getArtPieces = function () {
        var deferred = $q.defer();
        $http.get("/api/ArtPiece").success(function (data) {
            artPieces.length = 0;
            for (var a in data) {
                artPieces.push(data[a]);
            }
            deferred.resolve(artPieces);
        }).error(function (data, status) {
            console.log(status);
            deferred.reject();
        })
        return deferred.promise;
    }

    var getArtPiecesForArtist = function (id) {
        var artistPieces = [];
        for (var i = 0; i < artPieces.length; i++) {
            var piece = artPieces[i];
            if (piece.ArtistId == id) {
                artistPieces.push(piece);
            }
        }
        return artistPieces;
    }


    var addArtPiece = function (artPiece) {
        var deferred = $q.defer();
        $http.post("/api/ArtPiece", artPiece).success(function (data) {
            

            deferred.resolve();
        }).error(function (data, status) {
            console.log(status);
            deferred.reject();
        });
        return deferred.promise;
    }

    var addArtist = function (artist) {
        var deferred = $q.defer();
        $http.post("/api/Artist", artist).success(function (data) {
            //colleges.length = 0;

            deferred.resolve();
        }).error(function (data, status) {
            console.log(status);
            deferred.reject();
        });
        return deferred.promise;
    }


    return {
        getArtists: getArtists,
        getArtist: getArtist,
        getArtPiecesForArtist: getArtPiecesForArtist,
        getArtPieces: getArtPieces,
        addArtPiece: addArtPiece,
        addArtist: addArtist,
        isAdminLoggedIn: isAdminLoggedIn,
        isUserLoggedIn: isUserLoggedIn
    }

});


